package chuangyi.com.org.DOMIHome;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by zxh on 16/11/24.
 */
public class LoadingDialog extends Dialog {

    private Context context = null;
    private static LoadingDialog customProgressDialog = null;

    public LoadingDialog(Context context) {
        super(context);
        this.context = context;
    }

    public LoadingDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    public static LoadingDialog getInstance(Context context) {
        if (customProgressDialog == null) {
            customProgressDialog = new LoadingDialog(context, R.style.LoadingProgressDialog);
            //设置点击屏幕不消息
            customProgressDialog.setCancelable(false);
            customProgressDialog.setCanceledOnTouchOutside(false);
            //禁止返回键
            customProgressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                        return true;
                    } else {
                        return true; //默认返回 false，这里false不能屏蔽返回键，改成true就可以了
                    }
                }
            });

            customProgressDialog.setContentView(R.layout.dialog_loading);
            customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        } else {
            if(context.equals(customProgressDialog.context) == false){
                customProgressDialog = null;
                return getInstance(context);
            }
        }
        return customProgressDialog;
    }

//    public static void remove() {
//        try {
//            customProgressDialog.dismiss();
//            customProgressDialog.finalize();
//            customProgressDialog = null;
//        } catch (Throwable ex) {
//
//        }
//    }


    public void onWindowFocusChanged(boolean hasFocus) {

        if (customProgressDialog == null) {
            return;
        }

        ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();
    }

    /**
     * [Summary]
     * setTitile 标题
     *
     * @param strTitle
     * @return
     */
    public LoadingDialog setTitile(String strTitle) {
        return customProgressDialog;
    }

    /**
     * [Summary]
     * setMessage 提示内容
     *
     * @param strMessage
     * @return
     */
    public LoadingDialog setMessage(String strMessage) {
        TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.id_tv_loadingmsg);

        if (tvMsg != null) {
            tvMsg.setText(strMessage);
        }

        return customProgressDialog;
    }

    public void startProgressDialog() {
        customProgressDialog.setCancelable(false);
        customProgressDialog.show();
    }

    public void stopProgressDialog() {
        customProgressDialog.dismiss();
        customProgressDialog = null;
    }
}